/*
 * HC_SR04.c
 *
 *  Created on: Jul 22, 2024
 *      Author: Administrator
 */


#include "./HC_SR04/HC_SR04.h"
#include "usart.h"


float distant;      //测量距离
uint32_t measure_Buf[3] = {0};   //存放定时器计数值的数组
uint8_t  measure_Cnt = 0;    //状态标志位
uint32_t high_time;   //超声波模块返回的高电平时间


//===============================================读取距离
void SR04_GetData(void)
{
	switch (measure_Cnt){
	case 0:
		TRIG_H;
		delay_us(30);
		TRIG_L;

		measure_Cnt++;
		__HAL_TIM_SET_CAPTUREPOLARITY(&htim2, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_RISING);
		HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1);	//启动输入捕获       或者: __HAL_TIM_ENABLE(&htim5);
		break;
	case 3:
		high_time = measure_Buf[1]- measure_Buf[0];    //高电平时间
		printf("\r\n----valtage  high time -%d-us----\r\n",high_time);
		distant=(high_time*0.034)/2;  //单位cm
		///printf("\r\n- distance -%.2f-cm-\r\n",distant);
		//HAL_UART_Transmit(&huart1, (uint8_t *)distant, 3, 200);
		// 将 float 转换为字符串
//		char buffer[3]; // 存储转换后的字符串
//		sprintf(buffer, "%d cm", distant);
//		// 通过 UART 发送字符串
//		HAL_UART_Transmit(&huart1, (uint8_t *)buffer, strlen(buffer), 200);
		measure_Cnt = 0;  //清空标志位
		TIM2->CNT=0;     //清空计时器计数
		break;

	}
}


//===============================================us延时函数
void delay_us(uint32_t us)//主频72M
{
	uint32_t delay = (HAL_RCC_GetHCLKFreq() / 4000000 * us);
	while (delay--)
	{
		;
	}
}

//===============================================中断回调函数
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)//
{

	if(TIM2 == htim->Instance)// 判断触发的中断的定时器为TIM2
	{
		switch(measure_Cnt){
		case 1:
			measure_Buf[0] = HAL_TIM_ReadCapturedValue(&htim2,TIM_CHANNEL_1);//获取当前的捕获值.
			__HAL_TIM_SET_CAPTUREPOLARITY(&htim2,TIM_CHANNEL_1,TIM_ICPOLARITY_FALLING);  //设置为下降沿捕获
			measure_Cnt++;
			break;
		case 2:
			measure_Buf[1] = HAL_TIM_ReadCapturedValue(&htim2,TIM_CHANNEL_1);//获取当前的捕获值.
			HAL_TIM_IC_Stop_IT(&htim2,TIM_CHANNEL_1); //停止捕获   或者: __HAL_TIM_DISABLE(&htim5);
			measure_Cnt++;
		}
	}
}

int GetDistance(){
	int temp=distant;
	return temp;
}

/**
 * @description: HC-SR04触发
 * @param {*}
 * @return {*}
 */
void Hcsr04Start()
{
    HAL_GPIO_WritePin(Trig_GPIO_Port, Trig_Pin, GPIO_PIN_SET);
    delay_us(10);  //  10us以上
    HAL_GPIO_WritePin(Trig_GPIO_Port, Trig_Pin, GPIO_PIN_RESET);
}
